package priv.menghan.happy.learning.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.stereotype.Service;
import priv.menghan.happy.learning.common.util.PageUtils;

/**
 * 通用服务实现类，基于 MyBatis Plus 的 IService 接口。
 *
 * @author Monika
 * @since 2025/6/10
 */
@Service
public abstract class BaseServiceImpl<Req, Entity, Resp>
		implements BaseService<Req, Resp> {

	protected final IService<Entity> repository;
	protected final MapStruct<Entity, Resp> mapStruct;

	public BaseServiceImpl(IService<Entity> repository, MapStruct<Entity, Resp> mapStruct) {
		this.repository = repository;
		this.mapStruct = mapStruct;
	}

	/**
	 * 删除记录
	 */
	@Override
	public void del(Long id) {
		throw new UnsupportedOperationException("del method not implemented");
	}

	/**
	 * 新增记录
	 */
	@Override
	public void create(Req req) {
		throw new UnsupportedOperationException("create method not implemented");
	}

	/**
	 * 更新记录
	 */
	@Override
	public void update(Req req) {
		throw new UnsupportedOperationException("update method not implemented");
	}

	/**
	 * 查询详情
	 */
	@Override
	public Resp getDetail(Long id) {
		Entity entity = repository.getById(id);
		return mapStruct.toResp(entity);
	}

	/**
	 * 分页查询
	 */
	@Override
	public IPage<Resp> qry(Req req) {
		IPage<Entity> entityPage = repository.page(PageUtils.createPage(req));
		return entityPage.convert(mapStruct::toResp);
	}
}
